package com.purang.pms.aroutertest.map;

import android.content.Context;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.purang.pms.common.LocationUtils;
import com.purang.pms.common.MapService;

@Route(path = "/app/map/address")
public class MapServiceImpl extends MapService {

    private BDLocation mBDLocation;
    private LocationUtils mLocalService;
    private BDAbstractLocationListener mLocationListener;

    @Override
    public void startLocation(Context context) {
        mLocalService = LocationUtils.getInstance(context);
        mLocalService.start();
        mLocationListener = new BDAbstractLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (bdLocation != null && (bdLocation.getLocType() == BDLocation.TypeGpsLocation
                        || bdLocation.getLocType() == BDLocation.TypeNetWorkLocation
                        || bdLocation.getLocType() == BDLocation.TypeOffLineLocation)) {
                    mLocalService.unregisterListener(mLocationListener);
                    mLocalService.stop();
                    mBDLocation = bdLocation;
                }
            }
        };
        mLocalService.registerListener(mLocationListener);
    }

    @Override
    public void stopLocation() {
        if (mLocalService != null) {
            mLocalService.stop();
        }
    }

    @Override
    public BDLocation obtainBDLocation() {
        return mBDLocation;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLocalService != null) {
            mLocalService.unregisterListener(mLocationListener);
        }
    }
}
